/* Copyright 2001,2002,2003 NAH6
 * All Rights Reserved
 *
 * Parts Copyright DoD, Parts Copyright Starium
 *
 */
          /*LINTLIBRARY*/
          /*PROTOLIB1*/

#include <math.h>
#include "main.h"
#include "mexcite.h"

/**************************************************************************
*
* ROUTINE
*		ModifiedExcitation
*
* FUNCTION
*		Modify the stochastic code book excitation gain
*
* SYNOPSIS
*		ModifiedExcitation(LPC_res, Adapt_res, scale)
*
*   formal 
*
*                       data    I/O
*       name            type    type    function
*       -------------------------------------------------------------------
*	LPC_res		float	 i	LPC residual
*	Adapt_res	float	 i	Adaptive analysis residual
*	scale		float	 o	Stochastice codebook gain scale
*
*==========================================================================
*	
* DESCRIPTION
*
*	Depending on the current system state, the stochastic code book
*	excitation is reduced to a level that is low enough to produce
*	positive perceptual effects, yet is high enough so as not to upset
*	the dynamics of the system.  The main effect of the method is that
*	during sustained voiced sounds, the excitation level is attenuated.
*	In unvoiced and transition regions the level is amplified to a
*	level slightly more than that of standard CELP.
*
*	The relative adaptive code book excitation component is
*	increased in voiced regions by decreasing the stochastic code book
*	excitation component.  The amount of decrease in the stochastic
*	component depends on the efficiency of the adaptive component.
*	More reconstruction burden is placed on the adaptive component as
*	its efficiency increases.  The efficiency is measured by the
*	closeness (in the squareroot crosscorrelation sense) of the residual
*	signals before and after pitch prediction.  When the efficiency is 
*       high (e.g., > 0.9), the stochastic component is amplified slightly
*	(e.g., one quantizer level).
*
*	The procedure for modifying the stochastic gain outside the
*	search loop is:
*	1)  Measure the efficiency of the adaptive component (ccor)
*	2)  Search the stochastic code book for the optimum codeword
*	3)  Modify the stochastic code book gain
*
*	This method is compatible with Federal Standard 1016.
*
*	This code represents the modified excitation work done by R556 which
*	was inspired by the constrained excitation work done by Yair Shoham. 
*
*
*==========================================================================
*
* CALLED BY
*
*	Analysis
*
*==========================================================================
*
* REFERENCES
*
*	Shoham, Yair, "Constrained-Stochastic Excitation Coding of Speech
*	at 4.8 kbps," in Advances in Speech Coding, ed. B. Atal, V.
*	Cuperman, and A. Gersho, submitted to Kluwer Academic Publishers.
*
*	Shoham, Yair, "Constrained-Stochastic Excitation Coding of Speech," 
*	Abstracts of the IEEE Workshop on Speech Coding for
*	Telecommunications, 1989, p. 65.
*
***************************************************************************/
void ModifiedExcitation(
float	LPC_res[RES_LEN],
float	Adapt_res[RES_LEN],
float	*scale)
{
float	ccor, e1;
int	i;

/*  Calculate the Euclidean norm of the LPC residual */
	e1 = .000001;
	for(i=0; i<RES_LEN; i++)	{
	  e1 += LPC_res[i] * LPC_res[i];
	}

/*  Calculate the cross correlation of the LPC residual and the Adaptive analysis residual */
	ccor = .000001;
	for(i=0; i<RES_LEN; i++)	{
	  ccor += Adapt_res[i] * LPC_res[i];
	}

/*  Normalize the cross correlation */
	ccor /= e1;

/*  Square root cross correlation scaling */
	*scale = sqrt(fabs((double)(ccor)));

/*  Modify scale */
	if (*scale < 0.2)
	  *scale = 0.2;
	else
	  if(*scale > 0.9)
	    *scale *= 1.4;


}
